﻿#include <iostream>
#include <GL/glut.h>
#define PI 3.1415927
using namespace std;
class CP2 {
public:
	CP2() {};
public:
	int x, y;
};
class CTransform {    //二维几何变换  
public:
	CTransform(int first, int second, int third) { a = first; b = second; c = third; };
	void Identity();
	void Translate(double, double);    //平移变换矩阵
	void Rotate(double);                  //旋转变换矩阵
	void MultiMatrix();      //矩阵相乘
public:
	double T[3][3];
	int a, b, c;
};
void CTransform::Identity()//单位矩阵
{
	T[0][0] = 1.0; T[0][1] = 0.0; T[0][2] = 0.0;
	T[1][0] = 0.0; T[1][1] = 1.0; T[1][2] = 0.0;
	T[2][0] = 0.0; T[2][1] = 0.0; T[2][2] = 1.0;
}
void CTransform::Translate(double tx, double ty) //平移变换矩阵
{
	Identity();
	T[2][0] = tx;
	T[2][1] = ty;
	MultiMatrix();
}

void CTransform::Rotate(double beta)//旋转变换矩阵
{
	Identity();
	double rad = beta * PI / 180;
	T[0][0] = cos(rad); T[0][1] = sin(rad);
	T[1][0] = -sin(rad); T[1][1] = cos(rad);
	MultiMatrix();
}
void CTransform::MultiMatrix()//矩阵相乘
{
	int d, e, f;
	d = a * T[0][0] + b * T[1][0] + c * T[2][0];
	e = a * T[0][1] + b * T[1][1] + c * T[2][1];
	f = a * T[0][2] + b * T[1][2] + c * T[2][2];
	a = d; b = e; c = f;
}


void Midpoint(int x0, int y0, int x1, int y1, int p[][280])
{
	int flag = 0;
	int a, b, d, x, y;
	//k>1
	if (abs(x1 - x0) < abs(y1 - y0)) {
		swap(x0, y0);
		swap(x1, y1);
		flag = 1;
	}
	if (x0 > x1) {
		swap(x0, x1);
		swap(y0, y1);
	}

	a = y0 - y1;
	b = x1 - x0;
	d = 2 * a + b;
	if (y0 < y1) {//斜率为正
		x = x0;
		y = y0;
		//画点
		glVertex2f(x0, y0);
		CTransform C(x0, y0, 1);
		C.Translate(300, 100);
		glVertex2f(C.a, C.b);
		C.Rotate(135);
		glVertex2f(C.a, C.b);
		p[x0 + 140][y0 + 140] = 1;
		while (x < x1) {
			x++;
			if (d < 0) {
				y++;
				d += 2 * (a + b);
			}
			else {
				d += 2 * a;
			}
			if (flag == 1) {
				glVertex2f(y, x);
				CTransform C(y, x, 1);
				C.Translate(300, 100);
				glVertex2f(C.a, C.b);
				C.Rotate(135);
				glVertex2f(C.a, C.b);
				p[y + 140][x + 140] = 1;
			}
			else {
				glVertex2f(x, y);
				CTransform C(x, y, 1);
				C.Translate(300, 100);
				glVertex2f(C.a, C.b);
				C.Rotate(135);
				glVertex2f(C.a, C.b);
				p[x + 140][y + 140] = 1;
			}
		}
	}
	else {
		x = x1;
		y = y1;
		//画点
		glVertex2f(x0, y0);
		CTransform C(x0, y0, 1);
		C.Translate(300, 100);
		glVertex2f(C.a, C.b);
		C.Rotate(135);
		glVertex2f(C.a, C.b);
		p[x0 + 140][y0 + 140] = 1;
		while (x > x0) {
			x--;
			if (d < 0) {
				y++;
				d = d - 2 * a + 2 * b;
			}
			else {
				d = d - 2 * a;
			}
			if (flag == 1) {
				glVertex2f(y, x);
				CTransform C(y, x, 1);
				C.Translate(300, 100);
				glVertex2f(C.a, C.b);
				C.Rotate(135);
				glVertex2f(C.a, C.b);
				p[y + 140][x + 140] = 1;
			}
			else {
				glVertex2f(x, y);
				CTransform C(x, y, 1);
				C.Translate(300, 100);
				glVertex2f(C.a, C.b);
				C.Rotate(135);
				glVertex2f(C.a, C.b);
				p[x + 140][y + 140] = 1;
			}
		}
	}
}
void FloodFill4(int x, int y, int p[][280], int a, int b)
{

	if (x >= a && x <= b && p[x + 140][y + 140] != 1)   //未填充点属于区域内点oldcolor
	{
		glVertex2f(x, y);  //给像素赋新的颜色值
		CTransform C(x, y, 1);
		C.Translate(300, 100);
		glVertex2f(C.a, C.b);
		C.Rotate(135);
		glVertex2f(C.a, C.b);
		p[x + 140][y + 140] = 1;
		FloodFill4(x - 1, y, p, a, b);  //左  
		FloodFill4(x, y + 1, p, a, b); //上
		FloodFill4(x + 1, y, p, a, b);  //右
		FloodFill4(x, y - 1, p, a, b);   // 下
	}
}


void display(void) {
	int p[300][280] = { 0 };
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_POINTS);
	Midpoint(-140, -120, -100, -80, p);
	Midpoint(-100, -80, -80, -100, p);
	Midpoint(-80, -100, -120, -140, p);
	Midpoint(-120, -140, -140, -120, p);

	Midpoint(-40, -80, 0, -80, p);
	Midpoint(0, -80, 0, -140, p);
	Midpoint(0, -140, -40, -140, p);
	Midpoint(-40, -140, -40, -80, p);

	Midpoint(40, -80, 80, -80, p);
	Midpoint(80, -80, 80, -140, p);
	Midpoint(80, -140, 40, -140, p);
	Midpoint(40, -140, 40, -80, p);

	Midpoint(100, -100, 120, -80, p);
	Midpoint(120, -80, 160, -120, p);
	Midpoint(160, -120, 140, -140, p);
	Midpoint(140, -140, 100, -100, p);

	Midpoint(-140, 80, -20, 80, p);
	Midpoint(-20, 80, -20, 140, p);
	Midpoint(-20, 140, 20, 140, p);
	Midpoint(20, 140, 20, 80, p);
	Midpoint(20, 80, 120, 80, p);
	Midpoint(120, 80, 120, 60, p);
	Midpoint(-140, 60, -140, 80, p);

	Midpoint(-40, 60, -140, -20, p);
	Midpoint(-140, -20, -120, -40, p);
	Midpoint(-120, -40, -20, 40, p);
	Midpoint(-20, 40, -20, -40, p);
	Midpoint(-20, -40, 20, -40, p);
	Midpoint(20, -40, 20, 40, p);
	Midpoint(20, 40, 120, -40, p);
	Midpoint(120, -40, 140, -20, p);
	Midpoint(140, -20, 40, 60, p);
	Midpoint(-40, 60, -140, 60, p);
	Midpoint(40, 60, 120, 60, p);

	FloodFill4(-20, -100, p, -140, 160);
	FloodFill4(50, -100, p, -140, 160);
	FloodFill4(-101, -100, p, -140, 160);
	FloodFill4(130, -100, p, 100, 160);
	FloodFill4(45, 47, p, 40, 140);
	FloodFill4(-110, -20, p, -140, 40);
	FloodFill4(60, 70, p, 40, 140);

	glEnd();
	glFlush();
}

int main(int argc, char** argv) {
	glutInit(&argc, argv); //初始化glut
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	//设置窗口的模式－深度缓存，单缓存，颜色模型
	glutInitWindowPosition(200, 200); //设置窗口的位置
	glutInitWindowSize(500, 500); //设置窗口的大小
	glutCreateWindow("3D Tech- GLUT Tutorial"); //创建窗口并赋予title
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-500.0, 800.0, -500.0, 800.0);
	glutDisplayFunc(display);
	glutMainLoop();
}